import 'package:flutter/material.dart';

class HTabs extends StatefulWidget {
  final List<HTabItem> items;
  final int initialIndex;
  final ValueChanged<int>? onChanged;
  final bool isScrollable;
  final Color? indicatorColor;
  final Color? labelColor;
  final Color? unselectedLabelColor;
  final double? indicatorWeight;
  final EdgeInsetsGeometry? padding;

  const HTabs({
    super.key,
    required this.items,
    this.initialIndex = 0,
    this.onChanged,
    this.isScrollable = false,
    this.indicatorColor,
    this.labelColor,
    this.unselectedLabelColor,
    this.indicatorWeight,
    this.padding,
  });

  @override
  State<HTabs> createState() => _HTabsState();
}

class _HTabsState extends State<HTabs> with SingleTickerProviderStateMixin {
  late TabController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TabController(
      length: widget.items.length,
      vsync: this,
      initialIndex: widget.initialIndex,
    );
    _controller.addListener(_handleTabChange);
  }

  void _handleTabChange() {
    if (_controller.indexIsChanging) {
      widget.onChanged?.call(_controller.index);
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TabBar(
          controller: _controller,
          isScrollable: widget.isScrollable,
          indicatorColor: widget.indicatorColor,
          labelColor: widget.labelColor,
          unselectedLabelColor: widget.unselectedLabelColor,
          indicatorWeight: widget.indicatorWeight ?? 2.0,
          padding: widget.padding,
          tabs: widget.items.map((item) => item.tab).toList(),
        ),
        Expanded(
          child: TabBarView(
            controller: _controller,
            children: widget.items.map((item) => item.content).toList(),
          ),
        ),
      ],
    );
  }
}

class HTabItem {
  final Widget tab;
  final Widget content;

  const HTabItem({
    required this.tab,
    required this.content,
  });
}

// 预设样式的标签页
class HStyledTabs extends StatelessWidget {
  final List<HStyledTabItem> items;
  final int initialIndex;
  final ValueChanged<int>? onChanged;
  final bool isScrollable;

  const HStyledTabs({
    super.key,
    required this.items,
    this.initialIndex = 0,
    this.onChanged,
    this.isScrollable = false,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    
    return HTabs(
      items: items.map((item) => HTabItem(
        tab: Tab(
          text: item.title,
          icon: item.icon,
        ),
        content: item.content,
      )).toList(),
      initialIndex: initialIndex,
      onChanged: onChanged,
      isScrollable: isScrollable,
      indicatorColor: theme.primaryColor,
      labelColor: theme.primaryColor,
      unselectedLabelColor: theme.unselectedWidgetColor,
    );
  }
}

class HStyledTabItem {
  final String title;
  final Icon? icon;
  final Widget content;

  const HStyledTabItem({
    required this.title,
    this.icon,
    required this.content,
  });
} 